package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.NewsCategory;
import com.ysd.util.DBManager;

public class NewsCategoryDaoImpl {
	public List<NewsCategory> getNewsCategory() {
		List<NewsCategory> newList = new ArrayList<NewsCategory>();
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * FROM newscategory");
			while(rs.next()) {
				int newsCategoryId = rs.getInt("newsCategoryId");
				String newsCategoryName = rs.getString("newsCategoryName");
				
				
				NewsCategory newsCategory = new NewsCategory(newsCategoryId, newsCategoryName);
				newList.add(newsCategory);
			}
			return newList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	public List<NewsCategory> getNewsCategoryByPage(int page, int size){
		List<NewsCategory> newList = new ArrayList<NewsCategory>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql ="select*from newscategory limit "+(page-1)*size+","+size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int ncId = rs.getInt("newsCategoryId");
				String ncName = rs.getString("newsCategoryName");
				NewsCategory newsCategory = new NewsCategory(ncId, ncName);
				newList.add(newsCategory);
			}
			return newList;
		} catch (SQLException e) {
			e.printStackTrace();
		return null;
	}finally {
		DBManager.closeAll(conn, st, rs);
	}
	}
	
	
	
	public int delNewsCateById(int cId) {
		return DBManager.executeUpdate("delete from  newscategory where newscategoryId = "+cId);
	}
	
	public int addCategory(NewsCategory nc) {
		return DBManager.executeUpdate("insert into newscategory values (null,'"+nc.getNewsCategoryName()+"')");
	}

	public int delMessageByIds(String ids) {
		return DBManager.executeUpdate("delete from newscategory where newsCategoryId in("+ids+")");
		
	}
	
	public List<NewsCategory> getNewsCategoryName(String name){
		List<NewsCategory> newList = new ArrayList<NewsCategory>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * FROM newscategory where newscategoryname='"+name+"'");
			while(rs.next()) {
				int ncId = rs.getInt("newsCategoryId");
				String ncName = rs.getString("newsCategoryName");
				NewsCategory newsCategory = new NewsCategory(ncId, ncName);
				newList.add(newsCategory);
			}
			return newList;
		} catch (SQLException e) {
			e.printStackTrace();
		return null;
	}finally {
		DBManager.closeAll(conn, st, rs);
	}
	}
	
	public NewsCategory getNewsCategorById(int cateId) {
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		NewsCategory newsCategory=null;
		try {
			st = conn.createStatement();
			//rs = st.executeQuery("select * FROM newscategory where newscategoryId="+cateId);
			String sql = "select * FROM newscategory where newscategoryId="+cateId;
			
			rs=st.executeQuery(sql);
			while(rs.next()) {
				int ncId = rs.getInt("newsCategoryId");
				String ncName = rs.getString("newsCategoryName");
				 newsCategory = new NewsCategory(ncId, ncName);
				
			}
			System.out.println(newsCategory);
			return newsCategory;
			
		} catch (SQLException e) {
			e.printStackTrace();
		return null;
	}finally {
		DBManager.closeAll(conn, st, rs);
	}
}
	
	public int updateNewsCategoryById(NewsCategory nc) {
		String sql = "update newscategory set newsCategoryName='"+nc.getNewsCategoryName()+"' where newsCategoryId="+nc.getNewsCategoryId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	
}